
#import "SLFoundation.h"

NSString * getAppBundleDirectory(){
	return [[NSBundle mainBundle] resourcePath];
}

NSString * getAppDocumentDirectory(){
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

NSString * fileNameOfPath(NSString *path){
	NSRange range = [path rangeOfString:@"/" options:NSBackwardsSearch];
	if(range.location == NSNotFound){
		return [[path copy] autorelease];
	}
	
	NSUInteger index = range.location + 1;
	if(index >= [path length]){
		return nil;
	}
	
	return [path substringFromIndex:index];
}


NSString * urlEncode(NSString *str){
	NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
																			(CFStringRef)str, 
																			NULL, 
																			CFSTR(":/?#[]@!$&’()*+,;="), 
																			kCFStringEncodingUTF8);
	return [result autorelease];
	
}

NSString * trim(NSString *str){
	NSString *trimed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	if(trimed == nil || [trimed length] <= 0){
		return nil;
	}
	return trimed;
}

CGRect shrinkCGRect(CGRect rect, float shrinkW, float shrinkH){
	float w = rect.size.width;
	float h = rect.size.height;
	float x = rect.origin.x;
	float y = rect.origin.y;
	
	shrinkW = shrinkW > w/2 ? (w/2) : shrinkW;
	shrinkH = shrinkH > h/2 ? (h/2) : shrinkH;
	
	return CGRectMake(x+shrinkW, y+shrinkH, w-shrinkW*2, h-shrinkH*2);
}


NSString * formatMoney(double value){
	NSString *money = [NSString stringWithFormat:@"%.2f",value];
	int len = [money length];
	
	NSMutableString *str = [NSMutableString stringWithCapacity:len];
	
	NSString *intMoney = nil;
	int dotIndex = [money rangeOfString:@"."].location;
	if(dotIndex == NSNotFound){
		intMoney = [NSString stringWithString:money];
	}else{
		intMoney = [money substringWithRange:NSMakeRange(0, dotIndex)];
		[str appendString:[money substringWithRange:NSMakeRange(dotIndex, len-dotIndex)]];
	}
	
	int intLen = [intMoney length];
	
	int threeCounter = 0;
	
	for(int i=0; i<intLen; i++){
		int index = intLen - i - 1;
		NSString *c = [intMoney substringWithRange:NSMakeRange(index, 1)];
		[str insertString:c atIndex:0];
		threeCounter ++;
		
		if(threeCounter >= 3){
			threeCounter = 0;
			if(index > 0){
				[str insertString:@"," atIndex:0];
			}
		}
	}
	
	[str insertString:@"$ " atIndex:0];
	
	return [NSString stringWithString:str];
}

BOOL isCoordinateIn(double latN, double latS, double lngW, double lngE, double targetLat, double targetLng){
	
	BOOL isLatIn = NO;
	BOOL isLngIn = NO;
	
	isLatIn = (isLatIn <= latN) && (targetLat >= latS);
	
	double fLngW = lngW + 180;
	double fLngE = lngE + 180;
	double fTargetLng = targetLng + 180;
	if(fLngE >= fLngW){
		isLngIn = (fTargetLng <= fLngE) && (fTargetLng >= fLngW);
	}else{
		isLngIn = (fTargetLng >= fLngW) || (fTargetLng <= fLngE);
	}
	
	return isLatIn && isLngIn;
}



BOOL copyFileTo(NSString *sourcePath, NSString *destPath, BOOL isOverwrite){
	if(! [[NSFileManager defaultManager] fileExistsAtPath:sourcePath]){
		return NO;
	}
	
	NSError *err = nil;
	
	if([[NSFileManager defaultManager] fileExistsAtPath:destPath]){
		if(isOverwrite){
			[[NSFileManager defaultManager] removeItemAtPath:destPath error:&err];
			DebugLog(@"*** ERROR: copyFileTo, remove old error: %@",err);
		}else{
			return NO;
		}
	}
	
	err = nil;
	[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&err];
	
	if(err != nil){
		DebugLog(@"*** ERROR: copyFileTo, copy error: %@",err);
		return NO;
	}
	
	return YES;
}

